package com.ceba.test.modules.regist.dao;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import com.ceba.base.config.CommonConfig;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.regist.entity.GenterpriseLicList;
import cn.hutool.core.codec.Base64Encoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GenterpriseLicListDao extends IBaseDao{

    /**
     * 根据客户注册信息表id查所有lic导入记录
     * @param fkEnterpiseId
     * @return
     */
    public List<Map<String,Object>> queryEnterpriseListRegistLicAll(String fkEnterpiseId){
        Sql sql = new Sql("select gell.ID,e.ENTERPRISE_NAME,gell.LIC_NAME,gell.LIC_PATH,gell.CREATE_TIME,el.FK_ENTERPISE_ID,e.ENTERPRISE_ID " +
                "from g_enterprise_lic_list gell " +
                "LEFT JOIN g_enterprise_lic el " +
                "ON gell.ENTERPRISE_LIC_ID = el.ID " +
                "LEFT JOIN lic_enterprise e ON e.ID=el.FK_ENTERPISE_ID " +
                "WHERE el.FK_ENTERPISE_ID='"+fkEnterpiseId+"' ORDER BY CREATE_TIME DESC");
        DataTable dataTable = SqlSessionManager.getDataTable(sql);
        List<Map<String,Object>> genterpriseLicLists = new ArrayList<>();
        if(dataTable!=null&&dataTable.getRowCount()>0){
            for(int i=0;i<dataTable.getRowCount();i++){
                Map<String,Object> map = new HashMap<>();
                DataRow dataRow = dataTable.getRow(i);
                map.put("id",dataRow.getString("ID"));
                map.put("createTime",dataRow.getString("CREATE_TIME"));
                map.put("licName",dataRow.getString("LIC_NAME"));
                map.put("licPath",new Base64Encoder().encode(dataRow.getString("LIC_PATH").getBytes()));
                map.put("enterpriseId",dataRow.getString("ENTERPRISE_ID"));
                map.put("enterpriseName", CommonConfig.getEncode(dataRow.getString("ENTERPRISE_NAME")));
                map.put("fkEnterpiseId",dataRow.getString("FK_ENTERPISE_ID"));
                genterpriseLicLists.add(map);
            }
        }
        return genterpriseLicLists;
    }

    /**
     * 根据导入lic表id查询
     * @param id
     * @return
     */
    public Map<String,Object> queryGenterpriseLicListById(String id){
        Sql sql = new Sql("SELECT gell.ID,gell.LIC_NAME,gell.LIC_PATH,e.ENTERPRISE_ID " +
                "FROM g_enterprise_lic_list gell " +
                "LEFT JOIN g_enterprise_lic el ON gell.ENTERPRISE_LIC_ID = el.ID " +
                "LEFT JOIN lic_enterprise e ON el.FK_ENTERPISE_ID = e.ID " +
                "WHERE gell.ID='"+id+"'");
        Map<String,Object> map = new HashMap<>();
        DataTable dataTable = SqlSessionManager.getDataTable(sql);
        if(dataTable!=null&&dataTable.getRowCount()>0){
            DataRow dataRow = dataTable.getRow(0);
            map.put("id",dataRow.getString("ID"));
            map.put("licName",dataRow.getString("LIC_NAME"));
            map.put("licPath",dataRow.getString("LIC_PATH"));
            map.put("enterpriseId",dataRow.getString("ENTERPRISE_ID"));
        }
        return map;
    }



    /**
     *增加
     */

    public boolean  addGenterpriseLicList(GenterpriseLicList licList) throws  Exception{
        IStore store = StoreManager.create("g_enterprise_lic_list");
        IObject object = new XObject("g_enterprise_lic_list");
        object.put("ID", licList.getId());
        setObjectValue( object, licList);
        object.setCreateTime();
        object.setCreateUser();
        if(store.insert(object)>0){
            return true;
        }
        return false;
    }

    public void setObjectValue( IObject object,GenterpriseLicList licList){
        object.put("LIC_NAME",licList.getLicName());
        object.put("LIC_PATH",licList.getLicPath());
        object.put("ENTERPRISE_LIC_ID",licList.getEnterpriseLicId());
    }




}
